﻿// ***** Guidance generated code © Catharsis ***** //

using System;
using System.Collections.Generic;
using System.Linq;

using Castle.DynamicProxy;
using $prjsn$.Common;

using ProjectBase.Core;

namespace $safeprojectname$
{
    [Serializable]
    public class RestrictMetaDataFilter : IInterceptor
    {
        #region AOP

        public virtual void Intercept(IInvocation invocation)
        {
            // BindEntity
            invocation.Proceed();

            // post
            AfterExecuted(invocation);
        }

        protected virtual void AfterExecuted(IInvocation invocation)
        {
            var metaData = invocation.ReturnValue as MetaDataMessage;

            if (metaData.IsNull())
            {
                return;
            }

            int? id = 0;
            if (invocation.Arguments.IsNotEmpty())
            {
                id = invocation.Arguments.First() as int?;
            }

            // TODO Example
            //if (metaData.EntityName.IsEqual(Str.Controllers.Employee))
            //{
            //RestrictEmploye(metaData, id);
            //}

            // TODO
            // AuditChanges(entity, previous);
        }

        protected virtual void RestrictEmploye(MetaDataMessage metaData, int? id)
        {
            // TODO example - switch by culture, change to SecurityManager
            if (!System.Threading.Thread.CurrentThread.CurrentCulture.Name.StartsWith("cs"))
            {
                return;
            }
            MDAttribute attribute;
            attribute = metaData.GetAttribute("FirstName");
            attribute.CanRead = false;

            attribute = metaData.GetAttribute("LastName");
            attribute.CanEdit = false;

            attribute = metaData.GetAttribute("Login");
            attribute.CanQuery = false;
        }

        #endregion AOP
    }
}